<template>
  <div>
    <MainBox>
      <template #headerForm>
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="区域搜索">
            <el-input v-model="params.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #table>
        <el-button
          type="danger"
          icon="iconfont icon-zengjiatianjiajiahao"
          @click="showDialog"
        >新建</el-button>
        <el-table :data="list" style="width: 100% ">
          <el-table-column type="index" label="序号" width="80" />
          <el-table-column prop="name" label="区域名称" />
          <el-table-column prop="nodeCount" label="点位" />
          <el-table-column prop="remark" label="备注说明" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="text" @click="areaInfo(scope.row)">查看详情</el-button>
              <el-button type="text" @click="modifyFn(scope.row)">修改</el-button>
              <el-button type="text" class="del" @click="delFn(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="fotter">
          <div class="left">
            {{ totalNum }}
          </div>
          <div class="right">
            <el-button-group>
              <el-button :disabled="params.pageIndex === 1" type="primary" icon="el-icon-arrow-left" @click="prevFn">上一页</el-button>
              <el-button :disabled="params.pageIndex >= totalPage" type="primary" @click="nextFn">下一页<i class="el-icon-arrow-right el-icon--right" /></el-button>
            </el-button-group>
          </div>
        </div>
      </template>
    </MainBox>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="handleClose"
      @open="handleOpen"
    >
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="20%">
        <el-form-item label="区域名称:" prop="regionName">
          <el-input v-model="formData.regionName" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注说明:" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :rows="2"
            maxlength="40"
            show-word-limit
            placeholder="请输入备注（不超过40个字）"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addFn">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="区域详情"
      :visible.sync="isShowDialog"
      width="30%"
      @close="close"
    >
      <el-descriptions :column="1">
        <el-descriptions-item label="区域名称">{{ areaName }}</el-descriptions-item>
        <el-descriptions-item label="包含点位" label-style="{width:200px}">
          <el-table
            :data="tableData"
            style="width:70%"
            height="250"
          >
            <el-table-column
              type="index"
              label="序号"
              width="80"
            />
            <el-table-column
              prop="name"
              label="点位名称"
            />
            <el-table-column
              prop="vmCount"
              label="设备数量"
            />
          </el-table>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getAreaList, addArea, modifyArea, delArea, areaInfo, pointSearch } from '@/api'
export default {
  data() {
    return {
      isShowDialog: false,
      formData: {
        regionName: '',
        remark: ''
      },
      formRules: {
        regionName: [{ required: true, message: '此项不能为空', tigger: 'blur' }],
        remark: [{ required: true, message: '此项不能为空', tigger: 'blur' }]
      },
      // 新建  修改 对话框
      dialogVisible: false,
      // 接口数据
      params: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      // 表格底部显示数据
      totalNum: 0,
      // 数据总数
      totalCount: 0,
      // 数据页数
      totalPage: 0,
      // 列表数据
      list: [],
      areaName: '',
      tableData: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改区域' : '新增区域'
    }
  },
  created() {
    this.fetchAreaList()
  },
  methods: {
    close() {
      this.isShowDialog = false
    },
    async areaInfo(val) {
      this.isShowDialog = true
      this.areaName = val.name
      console.log(val)
      // this.params = val.name
      // console.log(this.params)
      const { data } = await areaInfo(val.id, {
        ...this.params,
        name: val.name
      })
      console.log(data)
      const res = await pointSearch({
        ...data,
        regionId: data.id,
        name: ''
      })
      // console.log(res)
      this.tableData = res.data.currentPageRecords
    },
    // 删除区域
    delFn(val) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '狠心删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await delArea(val)
          if (this.params.pageIndex > 1 && this.list.length === 1) this.params.pageIndex--
          this.fetchAreaList()
          this.$message.success('删除成功')
        } catch (error) {
          this.$message.error(error.response.data)
        }
      })
    },
    // modifyFn修改区域
    modifyFn(val) {
      console.log(val)
      this.dialogVisible = true
      this.formData = {
        ...val,
        regionName: val.name
      }
      // modifyArea(this.formData)
    },
    addFn() {
      this.$refs.formRef.validate(async volid => {
        if (!volid) return
        try {
          if (!this.formData.id) {
            await addArea(this.formData)
          } else {
            await modifyArea(this.formData)
          }
          this.$message.success(this.title + '成功')
          this.handleClose()
          // 刷新页面
          this.fetchAreaList()
        } catch (error) {
          this.$message.error(error.response.data)
          // console.dir(error)
        }
      })
    },
    // 新建函数
    showDialog() {
      this.dialogVisible = true
    },
    // 弹窗打开时的函数
    handleOpen() {

    },
    // 弹窗关闭
    handleClose() {
      this.dialogVisible = false
      this.$refs.formRef.resetFields()
    },
    // 翻页函数
    prevFn() {
      this.params.pageIndex > 1 ? this.params.pageIndex-- : ''
      this.fetchAreaList()
    },
    nextFn() {
      this.params.pageIndex < this.totalPage ? this.params.pageIndex++ : ''
      this.fetchAreaList()
    },
    onSubmit() {
      this.fetchAreaList()
    },
    // 获取列表
    async fetchAreaList() {
      const { data } = await getAreaList(this.params)
      // console.log(data)
      this.totalPage = data.totalPage
      this.totalNum = `共${data.totalCount}条记录  第${data.pageIndex}/${data.totalPage}页`
      this.list = data.currentPageRecords
    }
  }
}
</script>

<style lang="scss" scoped>
.el-button.del {
  color: red;
}
.el-descriptions-item__container{
.labelClassName {
  flex: 1;
  color: #909;
  background-color: #303;
}
}

</style>
